package application;

import utils.MessageHandler;
import utils.SynchQueue;

public class AppMessageHandler implements MessageHandler, Runnable {

	SynchQueue incoming = new SynchQueue();

	public void handleMessage(String msg) {
		incoming.add(msg);
	}

	public void run() {
		while (true) {
			String msg = incoming.get();
			String[] splitMessage = msg.split(":");
			if (splitMessage[2].equalsIgnoreCase("HEAD")) {
				ApplicationLayer.initializeReceptionWindow(splitMessage[1],
						Integer.parseInt(splitMessage[3]));
			} else if (splitMessage[2].equalsIgnoreCase("DATA")) {
				StringBuffer text = new StringBuffer();
				for (int i = 3; i < splitMessage.length; i++) {
					text.append(splitMessage[i]);
					if (i != splitMessage.length - 1) {
						text.append(":");
					}
				}
				ApplicationLayer.updateReceptionWindow(splitMessage[1], text
						.toString());
			}
		}
	}

}
